<!DOCTYPE HTML>
<html>
<link href="main.css" rel="stylesheet" type="text/css">

<title>SoftPlus</title><body>
<table border="0" align="center">
  <tr>
    <td width="150px"></td>
    <td width="1000px"><img src="banner-01n.gif" width="1000" height="90" alt="Softplus"></td>
  </tr>
  <tr>
    <td><?php
      require("navandset.php");
    ?></td>
<td valign="top">

<h1>Add/Edit Supplier Details</h1>

<?php
//Connect
$condet = GetGlobalConnectionOptions();
$con = mysql_connect($condet["server"],$condet["username"],$condet["password"]);
if (!$con)
{
	die("Could not connect: " . mysql_error());
}
//Select DB
mysql_select_db($condet["database"], $con);

//Code
//Create the array to store supplier details
$supp = array(
	"supplier" => "",
	"name" => "",
	"email" => ""
);

//Create a variable to hold a message that tells the user what has just happened, if something has happened
$retmessage = "";

if($_POST)
{
	//Get input from form
	$supp["supplier"] = strtoupper($_POST["supplier"]);
	$supp["name"] = $_POST["name"];
	$supp["email"] = $_POST["email"];

	//Load supplier details from database
	$qry = "SELECT * FROM tblsuppliers WHERE supplier='".$supp["supplier"]."';";
	$dsupp = mysql_fetch_array(mysql_query($qry));
	
	switch($_POST["operation"])
	{
		case "Load Supplier":
			//If tblsuppliers contains a record fot the input email, then load that into the $supp array
			if($dsupp)
			{
				$supp = $dsupp;
				$retmessage = "Loaded supplier details.";
			}
			else
			{
				$retmessage = "No records were found for supplier with code ".$supp["supplier"].".";
			};
			break;
		case "Save Supplier Details":
			//Check if all input fields have been filled
			if(
				$supp["supplier"] != "" and
				$supp["name"] != "" and
				$supp["email"] != "")
			{
				//If tblsuppliers contains a record fot the input email, then update it according to the input
				if($dsupp)
				{
					$qry = 	"UPDATE tblsuppliers ".
						"SET name='".$supp["name"].
						"', email='".$supp["email"].
						"' WHERE supplier='".$supp["supplier"]."'";
					mysql_query($qry);
					$retmessage = "Supplier details updated.".mysql_error();
				}
				//If tblsuppliers does not contain a record fot the input email, then insert the #supp array details
				else
				{
					$qry = 	"INSERT INTO tblsuppliers ".
						"VALUES ('".$supp["supplier"].
						"', '".$supp["name"].
						"', '".$supp["email"]."')";
					mysql_query($qry);
					if(mysql_error())
					{
						$retmessage = mysql_error();
					}
					else
					{
						$retmessage = "New supplier created."."(".$supp["supplier"].")";
					};
				};
			}
			else
			{
				//If any field is blank, then tell the user that they cannot save yet.
				$retmessage = "All fields marked with a * are mandatory";
			};
			break;
		case "Delete Supplier":
			if($dsupp)
			{
				//If the supplier record exists, then delete it.
				$qry = 	"DELETE FROM tblsuppliers ".
					"WHERE supplier='".$supp["supplier"]."'";
				mysql_query($qry);
				if(mysql_error())
				{
					$retmessage = mysql_error();
				}
				else
				{
					$retmessage = "Removed supplier record."."(".$supp["supplier"].")";
				};
			}
			else
			{
				$retmessage = "No records were found for supplier with code ".$supp["supplier"].".";
			};
			break;
		case "Clear Input Fields":
			//Set all fields to blank
			$supp["supplier"] = "";
			$supp["name"] = "";
			$supp["email"] = "";
			$retmessage = "Cleared input fields";
			break;
	};
};

?>

<form action="frmsupplier.php" method="post">
<table border="1">
      <tr>
        <td width="150px"><h2>*Supplier Code:</h2></td>
        <td><input type="text" name="supplier" size="100" maxlength="3"  <?php echo "value='".$supp["supplier"]."' "; ?> /></td>
        <td><input type="submit" name="operation" value="Load Supplier"></td>
      </tr>
      <tr>
        <td><h2>*Name:</h2></td>
        <td><input type="text" name="name" size="100" maxlength="30"  <?php echo "value='".$supp["name"]."' "; ?> /></td>
      </tr>
      <tr>
        <td><h2>*Email:</h2></td>
        <td><input type="text" name="email" size="100" maxlength="100"  <?php echo "value='".$supp["email"]."' "; ?> /></td>
      </tr>
</table>
<input type="submit" name="operation" value="Save Supplier Details">
<input type="submit" name="operation" value="Delete Supplier">
<input type="submit" name="operation" value="Clear Input Fields">
</form>
<h2>Fields marked with a * are required.</h2>
<h2><?php echo $retmessage ?></h2>


<?php
//Disconnect
mysql_close($con);
?>

</td>
  </tr>
</table>
</body>

</html>
